package cn.teduymdj.jxc.controller;


import cn.teduymdj.jxc.myAnnotation.LogAnnotation;
import cn.teduymdj.jxc.pojo.dto.GoodsAddNewDTO;
import cn.teduymdj.jxc.pojo.dto.GoodsUpdateInfoDTO;
import cn.teduymdj.jxc.pojo.entity.Log;
import cn.teduymdj.jxc.pojo.vo.GoodsInventoryListItemVO;
import cn.teduymdj.jxc.pojo.vo.GoodsListItemVO;
import cn.teduymdj.jxc.service.CustomerReturnListGoodsService;
import cn.teduymdj.jxc.service.GoodsService;
import cn.teduymdj.jxc.service.SaleListGoodsService;
import cn.teduymdj.jxc.web.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 商品信息Controller
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CustomerReturnListGoodsService customerReturnListGoodsService;

    @Autowired
    private SaleListGoodsService saleListGoodsService;

    /**
     * 分页查询商品库存信息
     *
     * @param page       当前页
     * @param rows       每页显示条数
     * @param codeOrName 商品编码或名称
     * @param typeId     商品类别ID
     * @return
     */
    //日志保存注解,不用管:李柯辰
    @LogAnnotation(content = "分页查询商品库存信息",type = Log.SELECT_ACTION)
    @RequestMapping("/listInventory")
    public JsonResult<List<GoodsInventoryListItemVO>> listInventory(Integer page, Integer rows, String codeOrName, Long typeId) {

        List<GoodsInventoryListItemVO> goodsInventoryList =
                goodsService.getGoodsInventoryList(page, rows, codeOrName, typeId);

        return JsonResult.ok(goodsInventoryList);
    }

    /**
     * 分页查询商品信息
     *
     * @param page   当前页
     * @param rows   每页显示条数
     * @param name   商品名称
     * @param typeId 商品类别ID
     * @return
     */
    @LogAnnotation(content = "分页查询商品信息",type = Log.SELECT_ACTION)
    @RequestMapping("/list")
    public JsonResult<List<GoodsListItemVO>> list(Integer page, Integer rows, String name, Long typeId) {
        List<GoodsListItemVO> goodsList = goodsService.getGoodsList(page, rows, name, typeId);
        return JsonResult.ok(goodsList);
    }

    /**
     * 生成商品编码
     *
     * @return
     */
    @RequestMapping("/getCode")
    public JsonResult<String> getCode() {

        //获取当前商品最大编码
        String code = goodsService.getMaxCode();
        //在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;
        //将编码重新格式化为4位数字符串形式
        StringBuilder unitCode = new StringBuilder(intCode.toString());

        for (int i = 4; i > intCode.toString().length(); i--) {
            unitCode.insert(0, "0");
        }
        return JsonResult.ok(unitCode.toString());
    }

    /**
     * 添加商品信息
     *
     * @param goodsAddNewDTO 商品信息实体
     * @return
     */
    @LogAnnotation(content = "添加商品信息",type = Log.INSERT_ACTION)
    @RequestMapping("/save")
    @ResponseBody
    public JsonResult<Void> save(GoodsAddNewDTO goodsAddNewDTO) {

        //设置上一次采购价为当前采购价
        goodsAddNewDTO.setLastPurchasingPrice(goodsAddNewDTO.getPurchasingPrice());
        goodsService.saveGoods(goodsAddNewDTO);
        return JsonResult.ok();
    }

    /**
     * 修改商品信息
     *
     * @param goodsUpdateInfoDTO 商品信息实体
     * @return
     */
    @LogAnnotation(content = "修改商品信息",type = Log.UPDATE_ACTION)
    @RequestMapping("/update")
    @ResponseBody
    public JsonResult<Void> update(GoodsUpdateInfoDTO goodsUpdateInfoDTO) {

        //设置上一次采购价为当前采购价
        goodsUpdateInfoDTO.setLastPurchasingPrice(goodsUpdateInfoDTO.getPurchasingPrice());
        goodsService.updateGoods(goodsUpdateInfoDTO);
        return JsonResult.ok();
    }

    /**
     * 删除商品信息
     *
     * @param goodsId 商品ID
     * @return
     */
    @LogAnnotation(content = "删除商品信息",type = Log.DELETE_ACTION)
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult<Void> delete(Long goodsId) {
        goodsService.findByGoodsId(goodsId);
        goodsService.deleteGoodsById(goodsId);
        return JsonResult.ok();

    }

    /**
     * 分页查询无库存商品信息
     *
     * @param page       当前页
     * @param rows       每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @LogAnnotation(content = "分页查询无库存商品信息",type = Log.SELECT_ACTION)
    @RequestMapping("/getNoInventoryQuantity")
    public JsonResult<List<GoodsListItemVO>> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<GoodsListItemVO> goodsList = goodsService.getNoInventoryQuantity(page, rows, nameOrCode);
        return JsonResult.ok(goodsList);
    }

    /**
     * 分页查询有库存商品信息
     *
     * @param page       当前页
     * @param rows       每页显示条数
     * @param nameOrCode 商品名称或商品编码
     * @return
     */
    @LogAnnotation(content = "分页查询有库存商品信息",type = Log.SELECT_ACTION)
    @RequestMapping("/getHasInventoryQuantity")
    public JsonResult<List<GoodsListItemVO>> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        List<GoodsListItemVO> goodsList = goodsService.getHasInventoryQuantity(page, rows, nameOrCode);
        return JsonResult.ok(goodsList);
    }

    /**
     * 添加商品期初库存
     *
     * @param id                商品ID
     * @param inventoryQuantity 库存
     * @param purchasingPrice   成本价
     * @return
     */
    @LogAnnotation(content = "添加商品期初库存",type = Log.INSERT_ACTION)
    @RequestMapping("/saveStock")
    @ResponseBody
    public JsonResult<Void> saveStock(Long id, Integer inventoryQuantity, BigDecimal purchasingPrice) {
        goodsService.findByGoodsId(id);
        GoodsAddNewDTO goodsAddNewDTO = new GoodsAddNewDTO();
        goodsAddNewDTO.setInventoryQuantity(inventoryQuantity);
        goodsAddNewDTO.setPurchasingPrice(purchasingPrice);
        goodsAddNewDTO.setLastPurchasingPrice(purchasingPrice);
        goodsService.saveGoods(goodsAddNewDTO);
        return JsonResult.ok();
    }

    /**
     * 删除商品库存
     *
     * @param id 商品ID
     * @return
     */
    @LogAnnotation(content = "删除商品库存",type = Log.DELETE_ACTION)
    @RequestMapping("/deleteStock")
    @ResponseBody
    public JsonResult<Void> deleteStock(Long id) {
        goodsService.findByGoodsId(id); //根据id查询商品
        GoodsListItemVO goodsListItemVO = new GoodsListItemVO();
        goodsListItemVO.setInventoryQuantity(0); //把商品数量设置为0
        goodsService.deleteGoodsById(id); //删除该商品
        return JsonResult.ok();
    }

    /**
     * 查询库存报警商品信息
     *
     * @return
     */
    @LogAnnotation(content = "查询库存报警商品信息",type = Log.SELECT_ACTION)
    @RequestMapping("/listAlarm")
    public JsonResult<List<GoodsListItemVO>> listAlarm() {
        List<GoodsListItemVO> goodsListItemVOS = goodsService.getGoodsAlarm();
        return JsonResult.ok(goodsListItemVOS);
    }

}
